package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.User;
import service.UserHandleService;
import service.impl.UserHandleServiceImpl;

public class ShowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserHandleService uhs = new UserHandleServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		List<User> users = uhs.loadUsers();
		PrintWriter writer = response.getWriter();
		StringBuilder sb = new StringBuilder();
		sb.append("<html>\r\n<head>\r\n<meta charset=\"UTF-8\">\r\n<title>用户信息</title>\r\n</head>\r\n<body>\r\n<table>\r\n");
		sb.append("<tr>\r\n<th>姓名</th>\r\n<th>工资</th>\r\n<th>生日</th>\r\n<th>性别</th>\r\n<th>头像</th>\r\n</tr>\r\n");

		for (User user : users) {
			sb.append("<tr>\r\n<td>" + user.getUname() + "</td>\r\n");
			sb.append("<td>" + user.getSal() + "</td>\r\n");
			sb.append("<td>" + user.getBirth() + "</td>\r\n");
			sb.append("<td>" + user.getSex() + "</td>\r\n");
			sb.append("<td>  <image width=30px height=30px src='handleImg?name="
					+ user.getUname() + "'  </td>\r\n");
			sb.append("</tr>\r\n");
		}
		sb.append("</table>\r\n</body>\r\n</html>");

		System.out.println(sb.toString());
		response.setContentType("text/html;charset=UTF-8");
		writer.write(sb.toString());
		writer.close();
	}

}
